<html>
<body>
<form action="calculator.php" method="GET">
<input type="text" name="expr" />
<input type="submit" value="Calculate"/>
</form>

<?php
if($_GET["expr"]) {
	$expr = $_GET["expr"];
	if (validateExpr($expr)) {
		//Mute errors such as 0/0
		@eval("\$ans = $expr;");
		echo $expr . " = " . $ans;
	}
	else {
		echo "Invalid input expression " . $expr . ".";
	}
}

function validateExpr($expr) {
	//Check for double minus sign exception (--)
	if (preg_match("/\-\-/", $expr, $matches))
		return false;
	if (preg_match("/^\-?\d?\.?\d+\s*([\/\*\\+\-]\s*\-?\d?\.?\d+\s*)*$/i", $expr, $matches))
		return true;
	return false;
}
?>

</body>
</html>